#include "CameraControl.h"

CameraControl::CameraControl(wxWindow* parent) :
	wxScrolled(parent)
{
	wxSizer* mainSizer = new wxFlexGridSizer(2,10,10);
	//labSensorHeight = new wxStaticText(this, wxID_ANY,wxT(""));
	//labSensorWidth = new wxStaticText(this, wxID_ANY, wxT(""));
	labCatchFrameRate = new wxStaticText(this, wxID_ANY, wxT(""));
	spinImageHeight = new wxSpinCtrl(this, (int)CameraControlID::spinImageHeight,wxEmptyString,wxDefaultPosition,wxDefaultSize, wxTE_PROCESS_ENTER);
	spinImageWidth = new wxSpinCtrl(this, (int)CameraControlID::spinImageWidth, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
	spinExposureTime = new wxSpinCtrlDouble(this, (int)CameraControlID::spinExposureTime);
	spinOffsetX = new wxSpinCtrl(this, (int)CameraControlID::spinOffsetX);
	spinOffsetY = new wxSpinCtrl(this, (int)CameraControlID::spinOffsetY);
	labSpeedLink = new wxStaticText(this, wxID_ANY, wxT(""));
	spinFrameRateControl= new wxSpinCtrl(this,(int)CameraControlID::spinFrameRateControl);
	spinCmpAcceleration=new wxSpinCtrl(this,(int)CameraControlID::spinCmpAcceleration);
	cboxTrigger=new wxCheckBox(this,(int)CameraControlID::cboxTrigger,wxT("硬件触发"));
	//mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("传感器高度")),wxSizerFlags().CenterVertical());
	//mainSizer->Add(labSensorHeight,wxSizerFlags().CenterVertical());
	//mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("传感器宽度")),wxSizerFlags().CenterVertical());
	//mainSizer->Add(labSensorWidth,wxSizerFlags().CenterVertical());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("捕获帧率")),wxSizerFlags().CenterVertical());
	mainSizer->Add(labCatchFrameRate,wxSizerFlags().CenterVertical());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("链路速率")),wxSizerFlags().CenterVertical());
	mainSizer->Add(labSpeedLink,wxSizerFlags().CenterVertical());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("图像高度")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinImageHeight, wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("图像宽度")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinImageWidth, wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("图像X偏移")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinOffsetX, wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("图像Y偏移")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinOffsetY, wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this, wxID_ANY, wxT("曝光时间")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinExposureTime, wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("压缩率")),wxSizerFlags().CentreVertical());
	mainSizer->Add(spinCmpAcceleration,wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("触发模式")),wxSizerFlags().CenterVertical());
	mainSizer->Add(cboxTrigger,wxSizerFlags().Expand());
	mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("帧率控制")),wxSizerFlags().CenterVertical());
	mainSizer->Add(spinFrameRateControl,wxSizerFlags().Expand());
	SetSizerAndFit(mainSizer);

	spinImageWidth->SetIncrement(8);
	spinImageHeight->SetIncrement(2);
	spinOffsetX->SetRange(0, 1000);
	spinOffsetY->SetRange(0, 1000);
	spinOffsetX->SetIncrement(8);
	spinOffsetY->SetIncrement(2);
	spinExposureTime->SetRange(20, 1000000);
	spinCmpAcceleration->SetRange(0,2000);
	spinCmpAcceleration->SetValue(100);
	spinFrameRateControl->SetValue(100);
	spinFrameRateControl->SetRange(1,10000);

	Bind(wxEVT_SPINCTRL,&CameraControl::on_spinFrameRateControl_change,this,(int)CameraControlID::spinFrameRateControl);
	Bind(wxEVT_CHECKBOX,&CameraControl::on_cboxTrigger_click,this,(int)CameraControlID::cboxTrigger);
	Bind(wxEVT_SPINCTRL, &CameraControl::on_spinImageHeight_change, this, (int)CameraControlID::spinImageHeight);
	Bind(wxEVT_SPINCTRL, &CameraControl::on_spinImageWidth_change, this, (int)CameraControlID::spinImageWidth);
	Bind(wxEVT_SPINCTRL, &CameraControl::on_spinImageOffsetX_change, this, (int)CameraControlID::spinOffsetX);
	Bind(wxEVT_SPINCTRL, &CameraControl::on_spinImageOffsetY_change, this, (int)CameraControlID::spinOffsetY);
	Bind(wxEVT_SPINCTRLDOUBLE, &CameraControl::on_spinExposureTime_textEnter, this, (int)CameraControlID::spinExposureTime);
	Bind(wxEVT_SPINCTRL,&CameraControl::on_spinCmpAcceleration_change,this,(int)CameraControlID::spinCmpAcceleration);
}

void CameraControl::SetCameraDevice(std::list<cameraDriver::cameraDevice>::iterator _device)
{
	CameraDevice = _device;
	UpdateCameraInf();
}

void CameraControl::UpdateCameraInf()
{
	//labSensorHeight->SetLabelText(wxString() << CameraDevice->GetSensorHeight());
	//labSensorWidth->SetLabelText(wxString() << CameraDevice->GetSensorWidth());
	labCatchFrameRate->SetLabelText(wxString() << CameraDevice->GetCollegeFrameRate());
	labSpeedLink->SetLabelText(wxString() << CameraDevice->GetLinkSpeed());
	spinImageHeight->SetRange(64,CameraDevice->GetSensorHeight());
	spinImageWidth->SetRange(64,CameraDevice->GetSensorWidth());
	//Debug
	spinImageHeight->SetValue(CameraDevice->GetImageHeight());
	spinImageWidth->SetValue(CameraDevice->GetImageWidth());
	spinExposureTime->SetValue(CameraDevice->GetExposureTime());
	spinOffsetX->SetValue(CameraDevice->GetImageOffsetX());
	spinOffsetY->SetValue(CameraDevice->GetImageOffsetY());
}

void CameraControl::on_spinImageHeight_change(wxSpinEvent& event)
{
	try
	{
		//必须是2的倍数
		CameraDevice->SetImageHeight(spinImageHeight->GetValue()/2*2);
	}
	catch(const std::exception& e)
	{
		wxMessageBox(wxString().FromUTF8(e.what()),wxT("操作失败"),wxICON_ERROR | wxOK);
	}
}

void CameraControl::on_spinImageWidth_change(wxSpinEvent& event)
{
	try
	{
		//必须是8的倍数
		CameraDevice->SetImageWidth(spinImageWidth->GetValue()/8*8);
	}
	catch(const std::exception& e)
	{
		wxMessageBox(wxString().FromUTF8(e.what()),wxT("操作失败"),wxICON_ERROR | wxOK);
	}

}

void CameraControl::UpdateFrameRate()
{
	labCatchFrameRate->SetLabelText(wxString() << CameraDevice->GetCollegeFrameRate());
}

void CameraControl::UpdateSpeedLink()
{
	labSpeedLink->SetLabelText(wxString() << CameraDevice->GetLinkSpeed());
}

void CameraControl::on_spinExposureTime_textEnter(wxSpinDoubleEvent& event)
{
	try
	{
		CameraDevice->SetExposureTime(spinExposureTime->GetValue());
	}
	catch(const std::exception& e)
	{
		wxMessageBox(wxString().FromUTF8(e.what()),wxT("操作失败"),wxICON_ERROR | wxOK);
	}
}

void CameraControl::on_spinImageOffsetX_change(wxSpinEvent& event)
{
	try
	{
		CameraDevice->SetImageOffsetX(spinOffsetX->GetValue());
	}
	catch(const std::exception& e)
	{
		wxMessageBox(wxString().FromUTF8(e.what()),wxT("操作失败"),wxICON_ERROR | wxOK);
	}
}

void CameraControl::on_spinImageOffsetY_change(wxSpinEvent& event)
{
	try
	{
		CameraDevice->SetImageOffsetY(spinOffsetY->GetValue());
	}
	catch(const std::exception& e)
	{
		wxMessageBox(wxString().FromUTF8(e.what()),wxT("操作失败"),wxICON_ERROR | wxOK);
	}
}

void CameraControl::disableSubFunOnCameraRunning()
{
	spinImageHeight->Enable(false);
	spinImageWidth->Enable(false);
	spinOffsetX->Enable(false);
	spinOffsetY->Enable(false);
	spinCmpAcceleration->Enable(false);
	cboxTrigger->Enable(false);
}

void CameraControl::enableSubFunOnCameraNotRunning()
{
	spinImageHeight->Enable(true);
	spinImageWidth->Enable(true);
	spinOffsetX->Enable(true);
	spinOffsetY->Enable(true);
	spinCmpAcceleration->Enable(true);
	cboxTrigger->Enable(true);
}

void CameraControl::on_spinCmpAcceleration_change(wxSpinEvent& event)
{
	CameraDevice->SetCmpAcceleration(spinCmpAcceleration->GetValue());
}

void CameraControl::on_cboxTrigger_click(wxCommandEvent& event)
{
	CameraDevice->SetTriggerMode(cboxTrigger->GetValue());
}

void CameraControl::on_spinFrameRateControl_change(wxSpinEvent& event)
{
	CameraDevice->SetImageFPSRate(spinFrameRateControl->GetValue());
}

int CameraControl::GetFrameRate()
{
	return spinFrameRateControl->GetValue();
}

void CameraControl::SetTriggerMode(bool mode)
{
	cboxTrigger->SetValue(mode);
	CameraDevice->SetTriggerMode(mode);
}

void CameraControl::SetExposeTime(const int32_t value)
{
	if(IsEnabled())
	{
		spinExposureTime->SetValue(value);
		CameraDevice->SetExposureTime(value);
	}
}

void CameraControl::SetImageHeight(const int32_t value)
{
	if(IsEnabled())
	{
		spinImageHeight->SetValue(value);
		CameraDevice->SetImageHeight(value);
	}
}

void CameraControl::SetImageWidth(const int32_t value)
{
	if(IsEnabled())
	{
		spinImageWidth->SetValue(value);
		CameraDevice->SetImageWidth(value);
	}
}

void CameraControl::SetImageOffsetX(const int32_t value)
{
	if(IsEnabled())
	{
		spinOffsetX->SetValue(value);
		CameraDevice->SetImageOffsetX(value);
	}
}

void CameraControl::SetImageOffsetY(const int32_t value)
{
	if(IsEnabled())
	{
		spinOffsetY->SetValue(value);
		CameraDevice->SetImageOffsetY(value);
	}
}

void CameraControl::SetFrameRate(const int32_t value)
{
	if(IsEnabled())
	{
		spinFrameRateControl->SetValue(value);
		CameraDevice->SetImageFPSControl(value);
	}
}
